#-- coding: utf8 --
from django.db import models
from django.contrib.auth.models import User
from datetime import timedelta

class BaseObject(models.Model):
    is_deleted = models.BooleanField(default = False)
    updated_date = models.DateField(null = True, auto_now = True)

    class Meta:
        abstract = True

class Project(BaseObject):
    name = models.CharField(max_length=128)
    description = models.TextField(max_length=2056, null = True)

    owner = models.ForeignKey(User, related_name = "owner")
    members = models.ManyToManyField(User)

    def __unicode__(self):
        return self.name

class Status(BaseObject):
    """Status of the task
    """

    name = models.CharField(max_length=128)
    description = models.TextField(max_length=2056, null = True)

    def __unicode__(self):
        return self.name
		
class Task(BaseObject):
    project = models.ForeignKey(Project)

    name = models.CharField(max_length=128)
    assignee = models.ForeignKey(User, related_name = "assignee")
    estimated_mds = models.FloatField(null = True, default=0)
    used_mds = models.FloatField(null = True, default=0)
    start_date = models.DateField(null = True)

    status = models.ForeignKey(Status, null = True)
    due_date = models.DateField(null = True)
    tag = models.CharField(max_length=128, null = True)
    note = models.CharField(max_length=2056, null = True)
    is_finished = models.BooleanField(default=False)

    def _get_end_date(self):
        if self.start_date != None:
            return self.start_date + timedelta(days = self.estimated_mds)
        else:
            return None

    end_date = property(_get_end_date)

    def __unicode__(self):
        return self.name

class HistoryItem(BaseObject):
    user_id = models.IntegerField()
    user_name = models.CharField(max_length=64)
    
    object_type_id = models.IntegerField()
    object_type_name = models.CharField(max_length=64)
    object_id = models.IntegerField()
    object_name = models.CharField(max_length=64)

    action_id  = models.IntegerField()
    action_name  = models.CharField(max_length=64)
    date_created = models.DateTimeField()
